package com.coolwen.springmvc.service;


import com.coolwen.springmvc.dao.IGroupDao;
import com.coolwen.springmvc.exception.UserException;
import com.coolwen.springmvc.model.Group;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author CoolWen
 * @version 2018-10-02 19:57
 */
@Service("groupService")
@Transactional(readOnly = true)
public class GroupServiceImpl implements GroupService {
    @Autowired
    private IGroupDao groupDao;

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void add(Group group) {
        groupDao.add(group);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void delete(int gid) {
        Group g = this.load(gid);
        if (g == null)
        {  throw new UserException("该组中有用户不能删除!");}
        groupDao.delete(gid);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
    public void update(Group group) {
        groupDao.update(group);
    }

    @Override
    public Group load(int gid) {
        return groupDao.load(gid);
    }


    @Override
    public List<Group> listAll() {
        String hql = "from Group";
        return groupDao.list(hql);
    }




}
